package com.rabbiter.pm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbiter.pm.domain.Fee;
import com.rabbiter.pm.domain.Sign;
import com.rabbiter.pm.domain.User;
import com.rabbiter.pm.domain.vo.CheckedVo;
import com.rabbiter.pm.mapper.FeeMapper;
import com.rabbiter.pm.mapper.SignMapper;
import com.rabbiter.pm.mapper.UserMapper;
import com.rabbiter.pm.service.FeeService;
import com.rabbiter.pm.service.SignService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rabbiter
 * @since 2023-03-27
 */
@Service
public class SignServiceImpl extends ServiceImpl<SignMapper, Sign> implements SignService {
    @Resource
    private SignMapper signMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public Object checkOut(CheckedVo checkedVo) {
        // 判断用户是否存在
        Map<String,Object> res = new HashMap<>();
        if(userMapper.selectById(checkedVo.getUid()) == null){
            res.put("code",400);
            res.put("flag",false);
            res.put("msg","用户不存在");
            return res;
        }
        // 判断用户是否已经签到
        QueryWrapper<Sign> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",checkedVo.getUid());

        Sign sign = signMapper.selectOne(wrapper);
        if (sign != null){
            res.put("code",400);
            res.put("flag",false);
            res.put("msg","用户已经签到");
            return res;
        }
        // 判断用户签到时间是否超过9点
        LocalDateTime now = LocalDateTime.now();
        if(now.getHour() > 9){
            res.put("code",400);
            res.put("flag",false);
            res.put("msg","签到时间已过");
            return res;
        }else{
            Sign sign1 = new Sign();
            sign1.setUid(checkedVo.getUid());
            sign1.setIsSign("1");
            sign1.setSignTime(LocalDateTime.now());
            int insert = signMapper.insert(sign1);
            if(insert > 0){
                res.put("code",200);
                res.put("flag",true);
                res.put("msg","签到成功");
            }
        }
        // 签到

        return res;
    }

    @Override
    public Object checkIn(CheckedVo checkedVo) {
        // 判断用户是否存在
        Map<String,Object> res = new HashMap<>();
        if(userMapper.selectById(checkedVo.getUid()) == null){
            res.put("code",400);
            res.put("flag",false);
            res.put("msg","用户不存在");
            return res;
        }
        // 判断用户是否已经签到
        QueryWrapper<Sign> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",checkedVo.getUid());
        Sign sign = signMapper.selectOne(wrapper);
        if (sign == null){
            // 判断补签时间是否超过了当天的12点
            LocalDateTime now = LocalDateTime.now();
            if(now.getHour() > 12){
                res.put("code",400);
                res.put("flag",false);
                res.put("msg","补签时间已过");
                return res;
            }else{
                // 补充签到
                Sign sign1 = new Sign();
                sign1.setUid(checkedVo.getUid());
                sign1.setSignTime(LocalDateTime.now());
                sign1.setIsSign("1");
                sign1.setSupplementaryTime(LocalDateTime.now());
                int insert = signMapper.insert(sign1);
                if(insert > 0){
                    res.put("code",200);
                    res.put("flag",true);
                    res.put("msg","补签成功");
                }
            }
            return res;
        }
        return res;
    }

    @Override
    public Object selectAll() {
        /**
         * 查询所有签到的用户
         */
        Map<String,Object> res = new HashMap<>();
        QueryWrapper<Sign> wrapper = new QueryWrapper<>();
        wrapper.eq("is_sign","1");
        // 查询用户信息
        // 查询签到过的用户的uid列表
        List<Sign> signs = signMapper.selectList(wrapper);
        if (signs.size() == 0){
            res.put("flag",false);
            res.put("msg","没有签到的用户");
            return res;
        }
        List<Integer> collect = signMapper.selectList(wrapper).stream()
                .map(Sign::getUid)
                .collect(Collectors.toList());

        // 根据uid查询用户详情
        List<User> userDetails = userMapper.selectBatchIds(collect);

        // 将用户详情放入res中
        List<User> usersWithoutPassword = new ArrayList<>();
        for (User user : userDetails) {
            // 删除密码字段
            user.setPassword(null);
            usersWithoutPassword.add(user);
        }
        res.put("data", signs);
        res.put("flag",true);
        return res;
    }

    @Override
    public Object deleteAll() {
        /**
         * 清空签到表
         */
        int i = signMapper.delete(null);
        Map<String,Object> res = new HashMap<>();
        res.put("flag",i>0);
        res.put("msg",i>0?"清空成功":"清空失败");
        return res;
    }
}
